from BSplineBase import BSplineBase
from utils import plotExampleBase,plot2DExample,plotSurface,generate_random_P
import numpy as np
定义B样条基函数
import numpy as np
class BSplineBase:
def __init__(self,U=np.asarray([0,0,0,0,0.5,1,1,1,1])):
self.U=U
# self.n=n
def GetValue(self,u,i,degree=3):
U=self.U
p=degree
if degree==0:
if u>=U[i] and u<=U[i+1]:
return 1
else:
return 0
else:
degree=degree-1
if u>=U[i] and u <=U[i+p+1]:
if U[i+p]!=U[i] and U[i+p+1]!=U[i+1]:
return (u-U[i])/(U[i+p]-U[i])*self.GetValue(u,i,degree)+(U[i+p+1]-u)/(U[i+p+1]-U[i+1])*self.GetValue(u,i+1,degree)
elif U[i+p]!=U[i]:
return (u-U[i])/(U[i+p]-U[i])*self.GetValue(u,i,degree)
elif U[i+p+1]!=U[i+1]:
return (U[i+p+1]-u)/(U[i+p+1]-U[i+1])*self.GetValue(u,i+1,degree)
else:
return 0
else:
return 0
U = np.array([0,0,0,0,0.2,0.4,0.6,0.8,0.8,0.8,1,1,1,1])
V = np.array([0,0,0,0,0.2,0.2,0.2,0.4,0.6,0.8,1,1,1,1])
绘制的B样条基函数如下图所示
U 方向
plotExampleBase(U=U,n=3)
V 方向
plotExampleBase(U=V,n=3)
plot2DExample(U,V,i=0,j=0)
plot2DExample(U,V,i=0,j=3)
plot2DExample(U,V,i=3,j=6)
随机生成的P
import numpy as np
import math
def generate_random_P():
P = []
for i in range(-5,5):
tmp = []
for j in range(-5,5):
tmp.append([i,j,10-math.floor((i*i+j*j)/20)+np.random.rand()])
P.append(tmp)
P=np.asarray(P)
return P
P = generate_random_P()
print(P)
开始绘制
plotSurface(U,V,P)